$(function() {
    $('.btn-login').click(function(event) {
        login_validator.resetForm();
        $('.header-login-form form').each (function(){
          this.reset();
        });
        $('.header-login-form .invalid_user').hide();
        $(".header-login-form").toggle(100);
        event.stopPropagation();
    });
    $('#mas-links-perfil').click(function(event) {
        $(".links-perfil-ocultos").toggle(100);
        event.stopPropagation();
    });

    $('.header-login-form').click(function(e){
        e.stopPropagation();
    })


    $(document).click(function() {
        //oculto elementos al hacerle click fueradel mismo
        $(".header-login-form").hide(100);
        $(".links-perfil-ocultos").hide();
    });
});

function sumarCreditos(){
  $.ajax({
    type: 'POST',
    url: '/admin/sumarCreditos',
    success: function(data) {
      $.jGrowl("Se te sumaron 20 creditos.", {theme: "msg-ok"});
      alert("Es solo para pruebas");
      window.location.reload();
    }
  });
}

function mostrarFormRegistro(){
    $('#registrar-modal').modal({
	opacity:50,
	overlayCss: {backgroundColor:"#000"}
    });
}

function initFacebook(appid){
    window.fbAsyncInit = function() {
            FB.init({appId: appid, status: true, cookie: true,xfbml: true});
    };
    var e = document.createElement('script');e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/es_LA/all.js';
    document.getElementById('fb-root').appendChild(e);
}

function registrarFacebook(){
    FB.login(function(response) {
        loadDataFb();
      }, {perms:'publish_stream,user_location,user_about_me,user_birthday,user_photos,email,user_interests'}
    );
}

function loginFacebook(appid,path){
    window.fbAsyncInit = function() {
        FB.init({appId: appid, status: true, cookie: true,xfbml: true});
        FB.login(function(response) {
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: path,
                success: function(data) {
                    if(data.valid == false){
                        mostrarFormRegistro();
                        registrarFacebook();
                    }else{
                        window.location.reload();
                    }
                }
            });
          }, {perms:'publish_stream,user_location,user_about_me,user_birthday,user_photos,email,user_interests'}
        );
    };
    var e = document.createElement('script');e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/es_LA/all.js';
    document.getElementById('fb-root').appendChild(e);
}

function loadDataFb(){
    FB.api('/me', function(response) {
        if(response.id)
            $('form input[name=fb_id]').val(response.id);

        if(response.email)
            $('form input[name=email]').val(response.email);

        if(response.first_name)
            $('form input[name=nombre]').val(response.first_name);

        if(response.gender && response.gender=='male')
        {
            $('.registrar_soy_h').attr("checked",true);
            $('.registrar_busco_m').attr("checked",true);
        }else{
            $('.registrar_soy_m').attr("checked",true);
            $('.registrar_busco_h').attr("checked",true);
        }
        if(response.bio)
        {
            $('form textarea[name=descripcion]').val(response.bio);
        }
        if(response.birthday)
        {
            fecha = response.birthday;
            fecha = fecha.split('/');
            $('form input[name=nacimiento]').val(fecha[1] + '/' + fecha[0] + '/' + fecha[2]);
        }
        if(response.location){
            $('form input[name=ubicacion_text]').autocomplete("search",response.location.name);
        }
    });
}